/** @file    wCore_Message.cpp
 *  @author  Alessandro Polo
 *  @version $Id: wCore_Message.cpp 2657 2010-06-15 19:57:29Z alex $
 *  @brief
 * File containing methods for the wosh::gui::wCore_Message class.
 * The header for this class can be found in wCore_Message.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "wCore_Message.h"

 #include "ui_wCore_Fact.h"
 #include "ui_wCore_Message.h"
 #include "ui_wCore_Request.h"
 #include "ui_wCore_Response.h"
 #include "ui_wCore_SecurityToken.h"

 #include <core/ObjectFactory.h>
 #include <framework/gui/GuiUtilities.h>

 #include <QtGui/QLabel>
 #include <QtGui/QTextEdit>


using namespace std;
namespace wosh {
 namespace gui {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::gui::wCore_Message, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Message )
 WOSH_GUI_MAP(wosh::gui::wCore_Message, "wosh::Message", 0.0, _static_Map_wCore_Message )

wCore_Message::wCore_Message( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_Message();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	loadObject(NULL);
 }

wCore_Message::~wCore_Message() {
	delete this->m_ui;
 }

void wCore_Message::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_Message::loadObject( const Message* message ) {
	if ( message == NULL ) {
		setWindowTitle( tr("%1 [NULL]").arg(getSerializableType()) );

		this->m_ui->cmbPriority->setCurrentIndex(0);

		this->m_ui->txtID->setText("");
		this->m_ui->txtRefID->setText("");
		this->m_ui->txtTS->setText("");
		this->m_ui->spinTTL->setValue(0);

		this->m_ui->wSourceURI->setTargetURI( URI() );
		this->m_ui->txtSourceBus->setText("");
		this->m_ui->txtSourceUUID->setText("");

		this->m_ui->wDestinationURI->setTargetURI( URI() );
		this->m_ui->txtDestinationBus->setText("");
		this->m_ui->txtDestinationUUID->setText("");

		this->m_ui->tabWidget->clear();
		return WRET_OK;
	 }
	setWindowTitle( tr("%1 #%2").arg(getSerializableType()).arg(message->getID()) );

	this->m_ui->txtID->setText( QString::number(message->getID()) );
	this->m_ui->txtRefID->setText( QString::number(message->getReferenceID()) );
	this->m_ui->txtTS->setText( message->getTimestampAsString().c_str() );
	this->m_ui->spinTTL->setValue( message->getTTL() );

	this->m_ui->wSourceURI->setTargetURI( message->getSource() );
	this->m_ui->txtSourceBus->setText( message->getSourceBus().c_str() );
	this->m_ui->txtSourceUUID->setText( message->getSourceUUID().c_str() );

	this->m_ui->wDestinationURI->setTargetURI( message->getDestination() );
	this->m_ui->txtDestinationBus->setText( message->getDestinationBus().c_str() );
	this->m_ui->txtDestinationUUID->setText( message->getDestinationUUID().c_str() );

	if ( message->isEmpty() ) {
		QLabel* tabContent_CT = new QLabel();
		tabContent_CT->setText( tr("Empty (NULL Content)") );
		tabContent_CT->setEnabled(false);
        this->m_ui->tabWidget->addTab(tabContent_CT, tr("Content [Empty]"));
	 }
	else {
		IWidgetSerializable* content_gui = GuiUtilities::createGuiFor<IWidgetSerializable>(message->getContent()->getClassName());
		if ( content_gui == NULL || content_gui->asQWidget() == NULL ) {
			QLabel* tabContent_CT = new QLabel();
			tabContent_CT->setText( tr("Unable to find GUI for %1").arg(message->getContent()->getClassName()) );
			tabContent_CT->setEnabled(false);
			this->m_ui->tabWidget->addTab(tabContent_CT, tr("Content [%1]").arg(message->getContent()->getClassName()) );
		 }
		else {
			if ( WFAILED( content_gui->loadSerializable(message->getContent()) ) ) {
				;// error
			 }
			this->m_ui->tabWidget->addTab(content_gui->asQWidget(), tr("Content [%1]").arg(message->getContent()->getClassName()) );
		 }
	 }

	if ( !message->hasSecurityToken() ) {
		QLabel* tabContent_ST = new QLabel();
		tabContent_ST->setText( tr("Empty (NULL SecurityToken)") );
		tabContent_ST->setEnabled(false);
        this->m_ui->tabWidget->addTab(tabContent_ST, tr("Security [Empty]"));
	 }
	else {
		IWidgetSerializable* token_gui = GuiUtilities::createGuiFor<IWidgetSerializable>(message->getSecurityToken()->getClassName());
		if ( token_gui == NULL || token_gui->asQWidget() == NULL ) {
			QLabel* tabContent_ST = new QLabel();
			tabContent_ST->setText( tr("Unable to find GUI for %1").arg(message->getSecurityToken()->getClassName()) );
			tabContent_ST->setEnabled(false);
			this->m_ui->tabWidget->addTab(tabContent_ST, tr("Security [%1]").arg(message->getSecurityToken()->getClassName()) );
		 }
		else {
			if ( WFAILED( token_gui->loadSerializable(message->getSecurityToken()) ) ) {
				;// error
			 }
			this->m_ui->tabWidget->addTab(token_gui->asQWidget(), tr("Security [%1]").arg(message->getSecurityToken()->getClassName()) );
		 }
	 }

	if ( message->hasNetworkForwarders() ) {
		QWidget* tabContent_NF = new QWidget();
        this->m_ui->tabWidget->addTab(tabContent_NF, tr("Network Forwarders"));
	 }

//	QTextEdit* tabContent_CRC = new QTextEdit();
//	tabContent_CRC->setPlainText( message->getCRC().c_str() );
//	tabContent_CRC->setEnabled(true);
//    this->m_ui->tabWidget->addTab(tabContent_CRC, tr("CRC"));

	return WRET_OK;
 }

WRESULT wCore_Message::saveObject( Message* message ) const {
	if ( message == NULL ) return WRET_ERR_PARAM;

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::gui::wCore_Fact, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Fact )
 WOSH_GUI_MAP(wosh::gui::wCore_Fact, "wosh::Fact", 0.0, _static_Map_wCore_Fact )

wCore_Fact::wCore_Fact( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_Fact();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	loadObject(NULL);
 }

wCore_Fact::~wCore_Fact() {
	delete this->m_ui;
 }

void wCore_Fact::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_Fact::loadObject( const wosh::Fact* fact ) {
	if ( fact == NULL ) {
		setWindowTitle( tr("%1 [NULL]").arg(getSerializableType()) );

		this->m_ui->txtEventName->setText("");

		return WRET_OK;
	 }
	setWindowTitle( tr("%1 #%2").arg(getSerializableType()).arg(fact->getEvent().c_str()) );

	this->m_ui->txtEventName->setText( fact->getEvent().c_str() );

	return WRET_OK;
 }

WRESULT wCore_Fact::saveObject( wosh::Fact* fact ) const {
	if ( fact == NULL ) return WRET_ERR_PARAM;
	fact->setEvent( this->m_ui->txtEventName->text().toLatin1().data() );
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::gui::wCore_Request, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Request )
 WOSH_GUI_MAP(wosh::gui::wCore_Request, "wosh::Request", 0.0, _static_Map_wCore_Request )

wCore_Request::wCore_Request( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_Request();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	loadObject(NULL);
 }

wCore_Request::~wCore_Request() {
	delete this->m_ui;
 }

void wCore_Request::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_Request::loadObject( const wosh::Request* request ) {
	if ( request == NULL ) {
		setWindowTitle( tr("%1 [NULL]").arg(getSerializableType()) );

		this->m_ui->txtMethodName->setText("");

		return WRET_OK;
	 }
	setWindowTitle( tr("%1 #%2").arg(getSerializableType()).arg(request->getMethod().c_str()) );

	this->m_ui->txtMethodName->setText( request->getMethod().c_str() );

	return WRET_OK;
 }

WRESULT wCore_Request::saveObject( wosh::Request* request ) const {
	if ( request == NULL ) return WRET_ERR_PARAM;
	request->setMethod( this->m_ui->txtMethodName->text().toLatin1().data() );
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::gui::wCore_Response, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_Response )
 WOSH_GUI_MAP(wosh::gui::wCore_Response, "wosh::Response", 0.0, _static_Map_wCore_Response )

wCore_Response::wCore_Response( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_Response();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	this->data_gui_missing = new QLabel(this);
	this->m_ui->verticalLayout->addWidget(this->data_gui_missing);
	this->data_gui = NULL;
	loadObject(NULL);
 }

wCore_Response::~wCore_Response() {
	delete this->m_ui;
 }

void wCore_Response::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_Response::loadObject( const wosh::Response* response ) {
	if ( response == NULL ) {
		setWindowTitle( tr("%1 [NULL]").arg(getSerializableType()) );

		this->m_ui->txtMethodName->setText("");
		this->m_ui->spinReturnValue->setValue(0);

		if ( this->data_gui != NULL ) {
			this->m_ui->verticalLayout->removeWidget(this->data_gui->asQWidget());
			ObjectFactory::destroy(this->data_gui);
			this->data_gui = NULL;
		 }

		return WRET_OK;
	 }
	setWindowTitle( tr("%1 #%2").arg(getSerializableType()).arg(response->getMethod().c_str()) );

	this->m_ui->txtMethodName->setText( response->getMethod().c_str() );
	this->m_ui->spinReturnValue->setValue( response->getReturnValue() );

	if ( !response->hasData() ) {
		if ( this->data_gui != NULL ) {
			this->m_ui->verticalLayout->removeWidget(this->data_gui->asQWidget());
			ObjectFactory::destroy(this->data_gui);
			this->data_gui = NULL;
		 }
		this->data_gui_missing->setText( tr("Emptry Data") );
		this->data_gui_missing->setVisible(true);
	 }
	else {
		this->data_gui_missing->setVisible(false);
		if ( this->data_gui != NULL && !this->data_gui->isSerializableType(response->getData()->getClassName()) ) {
			this->m_ui->verticalLayout->removeWidget(this->data_gui->asQWidget());
			ObjectFactory::destroy(this->data_gui);
			this->data_gui = NULL;
		 }
		if ( this->data_gui == NULL )
			this->data_gui = GuiUtilities::createGuiFor<IWidgetSerializable>(response->getData()->getClassName());

		if ( this->data_gui == NULL || this->data_gui->asQWidget() == NULL ) {
			this->data_gui_missing->setText( tr("Unable to find GUI for %1").arg(response->getData()->getClassName()) );
			this->data_gui_missing->setEnabled(false);
			this->data_gui_missing->setVisible(true);
		 }
		else {
			if ( WFAILED( this->data_gui->loadSerializable(response->getData()) ) ) {
				;// error
			 }
			this->m_ui->verticalLayout->addWidget(this->data_gui->asQWidget());
		 }
	 }

	return WRET_OK;
 }

WRESULT wCore_Response::saveObject( wosh::Response* response ) const {
	if ( response == NULL ) return WRET_ERR_PARAM;
	response->setMethod( this->m_ui->txtMethodName->text().toLatin1().data() );
	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::gui::wCore_SecurityToken, "wosh::gui::IWidgetSerializable", 1.0, _static_wCore_SecurityToken )
 WOSH_GUI_MAP(wosh::gui::wCore_SecurityToken, "wosh::SecurityToken", 0.0, _static_Map_wCore_SecurityToken )

wCore_SecurityToken::wCore_SecurityToken( QWidget* parent )
	: QWidget(parent) {

	this->m_ui = new Ui::wCore_SecurityToken();
	this->m_ui->setupUi(this);

	setWindowIcon( GuiUtilities::createIconFor( getSerializableType() ) );
	setWindowTitle( getSerializableType() );

	loadObject(NULL);
 }

wCore_SecurityToken::~wCore_SecurityToken() {
	delete this->m_ui;
 }

void wCore_SecurityToken::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT wCore_SecurityToken::loadObject( const SecurityToken* token ) {
	if ( token == NULL ) {
		setWindowTitle( tr("%1 [NULL]").arg(getSerializableType()) );

		this->m_ui->txtID->setText("");
		this->m_ui->txtTS->setText("");

		this->m_ui->txtUserName->setText("");
		this->m_ui->txtHostName->setText("");

		this->m_ui->txtCreatorUUID->setText("");
		this->m_ui->txtAuthenticatorClassName->setText("");
		this->m_ui->spinAuthenticatorVersion->setValue(0.0);

		return WRET_OK;
	 }
	setWindowTitle( tr("%1 #%2").arg(getSerializableType()).arg(token->getID()) );

	this->m_ui->txtID->setText( QString::number(token->getID()) );
	this->m_ui->txtTS->setText( QString::number(token->getTimeStamp()) );

	this->m_ui->txtUserName->setText( token->getUsername().c_str() );
	this->m_ui->txtHostName->setText( token->getCreatorHostName().c_str() );

	this->m_ui->txtCreatorUUID->setText( token->getCreatorUUID().c_str() );
	this->m_ui->txtAuthenticatorClassName->setText( token->getAuthenticatorName().c_str() );
	this->m_ui->spinAuthenticatorVersion->setValue( token->getAuthenticatorVersion() );

	return WRET_OK;
 }

WRESULT wCore_SecurityToken::saveObject( SecurityToken* ) const {
	return WRET_ERR_ILLEGAL_USE;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////



 }; // namespace gui
}; // namespace wosh
